package com.haidechizi.i18n.common.filter;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;
import com.haidechizi.i18n.common.config.Configuration;
import com.haidechizi.i18n.common.utils.GlobalUtils;

/**
 * 获取国际化语言的filter
 * 需要在国际化的filter之前执行
 */
@Activate(group = Constants.PROVIDER, order = -999)
public class I18nProviderFilter implements Filter {

    private Configuration configuration;

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String language = invocation.getAttachment(configuration.getI18nLocale(),
                configuration.getDefaultLanguage());
        GlobalUtils.setLanguage(language);
        return invoker.invoke(invocation);
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }
}
